/*
 * claplan_tree.cc
 *
 *  Created on: 4 Nov 2013
 *      Author: Tom Vercauteren
 */

#include <iostream>
#include "claplan_tree.h"

#include "debug.h"

using std::cout;
using std::endl;

ClaplanTree::ClaplanTree() :
		modelList(0)
{
	AddRoot(wxT("Claplans"));
}

ClaplanTree::ClaplanTree(wxWindow *parent, wxWindowID id, const wxPoint &pos,
		const wxSize &size, long style, const wxValidator &validator,
		const wxString &name) :
		wxTreeCtrl(parent, id, pos, size, style, validator, name), modelList(0)
{
	AddRoot(wxT("Claplans"));
}

ClaplanTree::~ClaplanTree()
{
//	unlinkModelList();
}

void ClaplanTree::update()
{
	ExpandAll();
}

void ClaplanTree::claplanDataChanged()
{
	here;
	update();
}

bool ClaplanTree::isClaplanFileSelected() const
{
	wxTreeItemId root = GetRootItem();
	wxTreeItemId selected = GetSelection();

	if (root.IsOk() && selected.IsOk())
	{
//		cout << GetItemText(selected).ToAscii().data() << endl;
		wxString rt = GetItemText(root);
		wxString st = GetItemText(selected);
		// selection is identical to root
		if (rt == st) return false;
		// Check all child item of the root
		unsigned i = 0, nr = GetChildrenCount(root, false);
		if (nr == 0) return false;
		wxTreeItemId item;
		wxTreeItemIdValue cookie;
		item = GetFirstChild(root, cookie);
		while (i < nr && item.IsOk())
		{
//			cout << "  - " << GetItemText(item).ToAscii().data() << endl;
			if (item == selected) return true;
			item = GetNextChild(root, cookie);
		}
	}
	return false;
}

int ClaplanTree::addClaplan(const std::string &name)
{
	wxTreeItemId root = GetRootItem();
	wxTreeItemId item;
	wxTreeItemIdValue cookie;
	wxString wxname = wxString(name.c_str(), wxConvUTF8);
	if (root.IsOk())
	{
		unsigned nr = GetChildrenCount(root, false), added = 0;
		if (nr == 0)
		{
			here;
			AppendItem(root, wxname);
			return 1;
		}
		else
		{
			item = GetFirstChild(root, cookie);
			while (item.IsOk() && added == 0)
			{
				if (GetItemText(item) == wxname) added = 1;
				item = GetNextChild(root, cookie);
			}
			if (added == 0)
			{
				AppendItem(root, wxname);
				return 1;
			}
		}
		return 0;
	}
	return -1;
}

int ClaplanTree::addClaplanSection(const std::string &name,
		const std::string &section)
{
	wxTreeItemId root = GetRootItem();
	wxTreeItemId item, subItem;
	wxTreeItemIdValue cookie, subCookie;
	wxString wxname = wxString(name.c_str(), wxConvUTF8);
	wxString wxsection = wxString(section.c_str(), wxConvUTF8);
	if (root.IsOk())
	{
		unsigned nr = GetChildrenCount(root, false);
		if (nr == 0) if (addClaplan(name) < 1) return -1;

		item = GetFirstChild(root, cookie);
		while (item.IsOk())
		{
			if (GetItemText(item) == wxname)
			{
				unsigned cnr = GetChildrenCount(item, false);
				if (cnr == 0)
				{
					AppendItem(item, wxsection);
					return 1;
				}
				subItem = GetFirstChild(item, subCookie);
				while (subItem.IsOk())
				{
					if (GetItemText(subItem) == wxsection) return 0;
					subItem = GetNextChild(item, subCookie);
				}
				AppendItem(item, wxsection);
				return 1;
			}
			item = GetNextChild(root, cookie);
		}
		return 0;
	}
	return -1;
}

int ClaplanTree::removeClaplan(const std::string &name)
{
	wxTreeItemId root = GetRootItem();
	wxTreeItemId item;
	wxTreeItemIdValue cookie;
	wxString wxname = wxString(name.c_str(), wxConvUTF8);
	if (root.IsOk())
	{
		unsigned nr = GetChildrenCount(root, false);
		if (nr == 0) return 0;
		else
		{
			item = GetFirstChild(root, cookie);
			while (item.IsOk())
			{
				if (GetItemText(item) == wxname)
				{
					DeleteChildren(item);
					Delete(item);
					return 1;
				}
				item = GetNextChild(root, cookie);
			}
		}
		return 0;
	}
	return -1;
}

int ClaplanTree::removeClaplanSection(const std::string &name,
		const std::string &section)
{
	wxTreeItemId root = GetRootItem();
	wxTreeItemId item, subItem;
	wxTreeItemIdValue cookie, subCookie;
	wxString wxname = wxString(name.c_str(), wxConvUTF8);
	wxString wxsection = wxString(section.c_str(), wxConvUTF8);
	if (root.IsOk())
	{
		unsigned nr = GetChildrenCount(root, false);
		if (nr == 0) return 0;

		item = GetFirstChild(root, cookie);
		while (item.IsOk())
		{
			if (GetItemText(item) == wxname)
			{
				subItem = GetFirstChild(item, subCookie);
				while (subItem.IsOk())
				{
					if (GetItemText(subItem) == wxsection)
					{
						DeleteChildren(subItem);
						Delete(subItem);
						return 1;
					}
					subItem = GetNextChild(item, subCookie);
				}
			}
			item = GetNextChild(root, cookie);
		}
		return 0;
	}
	return -1;
}

